package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.baiyang.server.model.system.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.dao.system.UserRoleDao;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.service.system.MenuRoleService;
import com.baiyang.server.service.system.MenuService;
import com.baiyang.server.service.system.UserRoleService;
import com.baiyang.server.service.system.UserService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.Data;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/menuOperation")
@Jurisdiction(value = "菜单信息表")
public class MenuCon {
	@Autowired
	private MenuService menuService = null;
	@Autowired
	private UserRoleService userRoleService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Menu> getPage(@RequestBody JSONObject object){
		Letter<Menu> letter = new Letter<Menu>();
		try {
			PageEntity<Menu> result = menuService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根据用户角色获取用户菜单信息
	 * @param request
	 * @return
	 * @apiNote 
	 * if(is admin)
	 * 		return  menu in ava = 1 and del = "0"
	 * else 
	 * 		menuRole(table) where role_id in [roles] and ava = "1" and del = "0"
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getMenus",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Menu> getMenus(HttpServletRequest request){
		Letter<Menu> letter = new Letter<Menu>();
		try {
			String userId  = DataTool.getUserIdFromSession(request);
			List<String> roleIds = userRoleService.selectIdsByUserId(userId);
			List<Menu> menus = menuService.getMenus(roleIds);
			Menu root = new Menu();
			root = (Menu) DataTool.getTree(menus, root);
			letter.setData(root);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	@Jurisdiction(value = "getAllMenus")
	@RequestMapping(value = "/getAllMenus", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Menu> getAllMenus() {
		Letter<Menu> letter = new Letter<Menu>();
		try {
			Menu menu = new Menu();
			List<Menu> menus = menuService.selectList(menu);
			Menu root = new Menu();
			root = (Menu) DataTool.getTree(menus, root);
			letter.setData(root);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}



		/**
	 * 根绝条件获取数据
	 * @return
	 */
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Menu> selectList(@RequestBody Menu menu){
		Letter<Menu> letter = new Letter<Menu>();
		try {
			
			List<Menu> result = menuService.selectList(menu);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根据条件获取一个
	 * @param menu
	 * @return
	 */
	@Jurisdiction(value = "读取")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Menu> selectOne(@RequestBody Menu menu){
		Letter<Menu> letter = new Letter<Menu>();
		try {
			Menu obj = menuService.selectOne(menu);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param menu
	 * @return
	 */
	@Jurisdiction(value = "新增")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Menu menu){
		Letter<String> letter = new Letter<String>();
		menu.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(menu);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			menuService.insert(menu);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 批量插入数据
	 * @param menus
	 * @return
	 */
	@Jurisdiction(value = "批量新增")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Menu> menus){
		Letter<String> letter = new Letter<String>();
		try {
			menuService.insertBatch(menus,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param menu
	 * @return
	 */
	@Jurisdiction(value = "删除")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Menu menu){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(menu)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			menuService.delete(menu);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param menus
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<Menu> menus) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(menus)) {
			letter.setDetail("请选中至少一行！");
			letter.setResult("FAIL");
			return letter;
		}
		try {
			menuService.deletes(menus);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝id更新
	 * @param menu
	 * @return
	 */
	@Jurisdiction(value = "修改")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Menu menu){
		Letter<String> letter = new Letter<String>();
		if(menu.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			menuService.updateById(menu);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;	
	}
}
